package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.DriverManagement;
import com.woniuxy.entity.dto.CarDTO;
import com.woniuxy.mapper.CarMapper;
import com.woniuxy.service.CarService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    private CarMapper carMapper;
    @Override
    public ResponseData<?> queryAllCar(PageData<CarDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<CarDTO> carDTOS = carMapper.queryAllCar(pageData.getParam());
        PageInfo pageInfo = new PageInfo(carDTOS);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> addCar(CarDTO carDTO) {
        carMapper.addCar(carDTO);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateStatus(CarDTO carDTO) {
        if (carDTO.getStatus().equals("0")){
            //上架 改成1

            carDTO.setStatus("1");
            carMapper.updateStatus(carDTO);
            return new ResponseData<>().ok();
        }else if (carDTO.getStatus().equals("1")){
            //下架  修改成0
            if (carDTO.getDriver() != null){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定司机 解绑停用车辆
            }
            carDTO.setStatus("0");
            carMapper.updateStatus(carDTO);
            return new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }

    @Override
    public ResponseData<?> queryAllDriver() {
        List<CarDTO> carDTOS = carMapper.queryAllDriver();
        return new ResponseData<>().ok(carDTOS);
    }

    @Override
    public ResponseData<?> updateDriver(CarDTO carDTO) {
        DriverManagement driverManagement = carMapper.queryLicenseType(carDTO.getDriverId());
        if (driverManagement.getLicenseType().equals("1")){
            if (!carDTO.getVehicleModelId().equals("3")){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定司机的驾照类型与车型不匹配
            }
        }
        if (driverManagement.getLicenseType().equals("2")){
            if (!carDTO.getVehicleModelId().equals("2")){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定司机的驾照类型与车型不匹配
            }
        }
        if (driverManagement.getLicenseType().equals("3")){
            if (!carDTO.getVehicleModelId().equals("1")){
                return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  绑定司机的驾照类型与车型不匹配
            }
        }
        carMapper.updateCarDriverId(carDTO);
        carMapper.updateDriverCarId(carDTO.getId().toString(),carDTO.getDriverId());
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> unBindDriver(CarDTO carDTO) {
        DriverManagement driverManagement = carMapper.queryDriverStatus(carDTO.getDriverId());
        if (!driverManagement.getStatus().equals("1")){
            return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);//2015  想解绑的司机正在运输中
        }
        carMapper.updateDriverCarIdNull(carDTO.getDriverId());
        carMapper.updateCarDriverIdNull(carDTO.getId().toString());
        return new ResponseData<>().ok();
    }
}
